﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Friso.Entities;
using Friso.Business;
using FetCo.Data;
using FetCo.Framework;
using System.IO;
using System.Data.SqlClient;
public partial class Fetco_Category : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["CurrentMenu"] = (int)Menus.Article;
        Session["SubCurrentMenu"] = (int)SubMenus.ListCategory;
        if (IsPostBack)
            return;
        TreeView1.Nodes.Clear();
        FillCategorys();
        TreeView1.ExpandAll();
    }
    protected void Node_Populate(object sender, System.Web.UI.WebControls.TreeNodeEventArgs e)
    {
    }
    protected void FillCategorys()
    {
        TreeNode node = new TreeNode("PRISO", "0");
        TreeView1.CssClass = "blue";
        TreeView1.Nodes.Add(node);
        string connString = ConfigurationManager.ConnectionStrings["DefaultDatabase"].ToString();
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand("Select * From Category where 1=1 order by [dbo].[Category].[Order]", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet categorys = new DataSet();
        adapter.Fill(categorys);
        if (categorys.Tables.Count > 0)
        {
            foreach (DataRow row in categorys.Tables[0].Rows)
            {
                TreeNode newNode = new
                TreeNode(row["Name"].ToString(), row["CategoryID"].ToString());
                //newNode.PopulateOnDemand = true;
                newNode.SelectAction = TreeNodeSelectAction.Expand;
                newNode.NavigateUrl = "category.aspx?id=" + newNode.Value.ToString();
                node.ChildNodes.Add(newNode);
                newNode.Collapse();
                FillSubCategorys(newNode);
            }
        }
    }
    protected void FillSubCategorys(TreeNode node)
    {
        string CategoryID = node.Value;
        string connString = ConfigurationManager.ConnectionStrings["DefaultDatabase"].ToString();
        SqlConnection connection = new SqlConnection(connString);
        SqlCommand command = new SqlCommand("Select * From [dbo].[SubCategory] " + " Inner Join [dbo].[Category] on [dbo].[SubCategory].[CategoryID] = [dbo].[Category].[CategoryID] " + " Where [dbo].[SubCategory].[CategoryID] = " + CategoryID + " and [dbo].[SubCategory].[SubCategorylevel] =0" + " order by [dbo].[SubCategory].[Order] ", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet SubCategorys = new DataSet();
        adapter.Fill(SubCategorys);
        if (SubCategorys.Tables.Count > 0)
        {
            foreach (DataRow row in SubCategorys.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["Name"].ToString(), row["SubCategoryID"].ToString());
                newNode.PopulateOnDemand = false;
                newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                newNode.NavigateUrl = "Subcategory.aspx?id=" + newNode.Value.ToString();
                node.ChildNodes.Add(newNode);
                FillSubCategoryChilds(newNode, long.Parse(CategoryID));
                newNode.Collapse();
            }
        }
    }
    protected void FillSubCategoryChilds(TreeNode node, long CategoryID)
    {
        string strSubCategoryID = node.Value;
        string connString = ConfigurationManager.ConnectionStrings["DefaultDatabase"].ToString();
        SqlConnection connection = new SqlConnection(connString);
        //SqlCommand command = new SqlCommand("Select * From SubCategory T" + " Inner Join Category TA on T.CategoryID = TA.CategoryID " + " Where TA.CategoryID = " + CategoryID.ToString() + " and T.SubCategorylevel =1 and T.ParrentID=" + node.Value + " order by T.Order ", connection);
        SqlCommand command = new SqlCommand("Select * From [dbo].[SubCategory] " + " Inner Join [dbo].[Category] on [dbo].[SubCategory].[CategoryID] = [dbo].[Category].[CategoryID] " + " Where [dbo].[SubCategory].[CategoryID] = " + CategoryID + " and [dbo].[SubCategory].[SubCategorylevel] =1 and [dbo].[SubCategory].[ParrentID]= " + node.Value + " order by [dbo].[SubCategory].[Order] ", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet SubCategorys = new DataSet();
        adapter.Fill(SubCategorys);
        if (SubCategorys.Tables.Count > 0)
        {
            foreach (DataRow row in SubCategorys.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["Name"].ToString(), row["SubCategoryID"].ToString());
                newNode.PopulateOnDemand = false;
                newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                newNode.NavigateUrl = "Subcategory.aspx?id=" + newNode.Value.ToString();
                node.ChildNodes.Add(newNode);
                FillSubCategoryParentChilds(newNode, CategoryID);
            }
        }
    }
    protected void FillSubCategoryParentChilds(TreeNode node, long CategoryID)
    {
        string strSubCategoryID = node.Value;
        string connString = ConfigurationManager.ConnectionStrings["DefaultDatabase"].ToString();
        SqlConnection connection = new SqlConnection(connString);
        //SqlCommand command = new SqlCommand("Select * From SubCategory T" + " Inner Join Category TA on T.CategoryID = TA.CategoryID " + " Where TA.CategoryID = " + CategoryID.ToString() + " and T.SubCategorylevel =1 and T.ParrentID=" + node.Value + " order by T.Order ", connection);
        SqlCommand command = new SqlCommand("Select * From [dbo].[SubCategory] " + " Inner Join [dbo].[Category] on [dbo].[SubCategory].[CategoryID] = [dbo].[Category].[CategoryID] " + " Where [dbo].[SubCategory].[CategoryID] = " + CategoryID + " and [dbo].[SubCategory].[SubCategorylevel] =2 and [dbo].[SubCategory].[ParrentID]= " + node.Value + " order by [dbo].[SubCategory].[Order] ", connection);
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet SubCategorys = new DataSet();
        adapter.Fill(SubCategorys);
        if (SubCategorys.Tables.Count > 0)
        {
            foreach (DataRow row in SubCategorys.Tables[0].Rows)
            {
                TreeNode newNode = new TreeNode(row["Name"].ToString(), row["SubCategoryID"].ToString());
                newNode.PopulateOnDemand = false;
                newNode.SelectAction = TreeNodeSelectAction.SelectExpand;
                newNode.NavigateUrl = "Subcategory.aspx?id=" + newNode.Value.ToString();
                node.ChildNodes.Add(newNode);
                //FillSubCategoryParentChilds(newNode, CategoryID);
            }
        }
    }
    protected void TreeView1_TreeNodeDataBound(object sender, TreeNodeEventArgs e)
    {
        if (e.Node.Text == "Food Lovers")  
         {  
            e.Node.Parent.ChildNodes.Remove(e.Node);  
         }  
    }
    protected void lbtDelete_OnClick(object sender, EventArgs e)
    {
        foreach (TreeNode parent in TreeView1.Nodes)
        {
            if (parent.Checked)
            {
                TreeView1.Nodes.Remove(parent);
            }
            else
            {
                foreach (TreeNode child in parent.ChildNodes)
                {
                    if (child.Checked)
                    {
                        if (child.Depth == 1)
                        {
                            DeleteCat(long.Parse(child.Value));
                        }
                        if (child.Depth > 1)
                        {
                            DeleteSub(long.Parse(child.Value));
                        }
                    }
                    else
                    {
                        foreach (TreeNode childlevel1 in child.ChildNodes)
                        {
                            if (childlevel1.Checked)
                            {
                                DeleteSub(long.Parse(childlevel1.Value));
                            }
                            else
                            {
                                foreach (TreeNode childlevel2 in childlevel1.ChildNodes)
                                {
                                    if (childlevel2.Checked)
                                    {
                                        DeleteSub(long.Parse(childlevel2.Value));
                                    }
                                    else
                                    {
                                        foreach (TreeNode childlevel3 in childlevel2.ChildNodes)
                                        {
                                            if (childlevel3.Checked)
                                            {
                                                DeleteSub(long.Parse(childlevel3.Value));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                   
                }
            }
        }
        Response.Redirect("CategoryList.aspx");
        
    }
    protected void DeleteCat(long catID)
    {
        Criteria cri = new Criteria();
        cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", catID, Condition.EQUAL, Condition.AND);
        SubCategoryCollection _SubCategoryCollection = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", OrderDirection.ASC, cri.Criter);
        if (_SubCategoryCollection.Count > 0)
        {
            foreach (SubCategory _SubCategory in _SubCategoryCollection)
                SubCategoryService.DeleteSubCategory(_SubCategory.SubCategoryID);
        }
        CategoryService.DeleteCategory(catID);
    }
    protected void DeleteSub(long subID)
    {
        Criteria cri = new Criteria();
        cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", subID, Condition.EQUAL, Condition.AND);
        SubCategoryCollection _SubCategoryCollection = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", OrderDirection.ASC, cri.Criter);
        if (_SubCategoryCollection.Count > 0)
        {
            foreach (SubCategory _SubCategory in _SubCategoryCollection)
            {
                cri = new Criteria();
                cri.add("[" + SubCategory._Table + "].[" + SubCategoryColumns.Parrentid + "]", _SubCategory.SubCategoryID, Condition.EQUAL, Condition.AND);
                SubCategoryCollection _SubCategoryCollectionSub = SubCategoryService.GetSubCategoryListByParam("[" + SubCategory._Table + "].[" + SubCategoryColumns.CategoryID + "]", OrderDirection.ASC, cri.Criter);
                foreach (SubCategory _SubCategorySub in _SubCategoryCollectionSub)
                {
                    SubCategoryService.DeleteSubCategory(_SubCategorySub.SubCategoryID);
                }
                SubCategoryService.DeleteSubCategory(_SubCategory.SubCategoryID);
            }
        }
        SubCategoryService.DeleteSubCategory(subID);
    }
}
